package com.zhangmeng.tools.utils;

/**
 * @author : 芊芊墨客
 * @version : 1.0
 * @date : 2023-03-02 11:40
 */
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.util.Map;

public class JsoupUtil {

    public static Document sendGet(String url, Integer timeOut) throws IOException {
        return Jsoup.connect(url).timeout(timeOut).ignoreContentType(true)
                .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                .header("Accept-Encoding", "gzip, deflate, sdch")
                .header("Accept-Language", "zh-CN,zh;q=0.8")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
                .get();
    }
    public static Document sendPost(String url, Integer timeOut, Map<String, String> map) throws IOException {
        return Jsoup.connect(url).timeout(timeOut).ignoreContentType(true)
                .header("Accept", "application/json, text/javascript, */*; q=0.01")
                .header("Accept-Encoding", "gzip, deflate")
                .header("Accept-Language", "zh-CN,zh;q=0.9")
                .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36")
                .data(map)
                .post();
    }
    public static Document sendPostByBody(String url, Integer timeOut, String json) throws IOException {
        return Jsoup.connect(url).timeout(timeOut).ignoreContentType(true)
                .header("Accept", "application/json, text/javascript, */*; q=0.01")
                .header("Accept-Encoding", "gzip, deflate")
                .header("Accept-Language", "zh-CN,zh;q=0.9")
                .header("Content-Type", "application/json;charset=utf-8")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36")
                .requestBody(json)
                .post();
    }

//    public static void main(String[] args) {
//        try {
//            Document document = sendGet("https://so.gushiwen.cn/shiwenv_45c396367f59.aspx", 5000);
//            Element body = document.body();
//            Element sonsyuanwen = body.getElementById("sonsyuanwen");
//            String title = sonsyuanwen.getElementsByTag("h1").get(0).text();
//            String user_name = sonsyuanwen.getElementsByTag("p").get(0).getElementsByTag("a").get(0).text();
//            String date_name = sonsyuanwen.getElementsByTag("p").get(0).getElementsByTag("a").get(1).text();
//            String content = sonsyuanwen.getElementsByClass("cont").get(0).getElementsByClass("contson").text();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
}
